package cn.ac.iscas.action.user;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.ac.iscas.bean.PageView;
import cn.ac.iscas.bean.project.Project;
import cn.ac.iscas.bean.user.User;
import cn.ac.iscas.service.user.IUserService;

@Controller @Scope("prototype")
public class UserAction {

	@Resource IUserService userService;
	private UserQuery userQuery;
	private User user;
	private int page;
	
	public String register(){
		return "add";
	}
	public String save(){
		User u=userService.checkUserName(user.getUserName());
		if(u!=null)
			return "addFai";
		userService.save(user);
		return "addSuc";
	}
//	public String login(){
//		return "login";
//	}
//	public String checkUser(){
//		User u=userService.checkUser(user.getUserName(), user.getPassword());
//		if(u!=null)
//			return "loginSuc";
//		return "loginFai";
//	}
	public String edit(){
		user=userService.get(User.class, user.getId());
		return "edit";
	}
	public String update(){
		userService.update(user);
		return "updateSuc";
	}
	public String list()
	{
		page = (0==page)?1:page;
		PageView<User> pageView = new PageView<User>(7, page);
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("id", "desc");
		pageView.setQueryResult(userService.getScrollData(User.class,pageView.getFirstResult(),pageView.getMaxresult(), orderby));
		ActionContext.getContext().put("pageView", pageView);
		return "list";
	}
	public String delete() throws IOException{
		System.out.println("haha");
		user = userService.get(User.class,user.getId());
		userService.delete(user);
		ActionContext ctx = ActionContext.getContext();     
	    HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
	    response.setContentType("text/html;charset=utf-8");
	    PrintWriter out=response.getWriter();
	    response.setContentType("text/html;charset=utf-8");
	    out.println("成功删除");
		return null;
	}
	public String queryPage(){
		return "query";
	}
	public String query(){
		if(page<=0)page=1;
		StringBuffer jpql = new StringBuffer("");
		List<Object> params = new ArrayList<Object>();
		if(userQuery.getUserName()!=null && !"".equals(userQuery.getUserName())){
			if(params.size()>0) jpql.append(" and ");
			jpql.append(" o.userName like ?");
			params.add("%"+userQuery.getUserName()+"%");
		}
		if(userQuery.getRealName()!=null && !"".equals(userQuery.getRealName())){
			if(params.size()>0) jpql.append(" and ");
			System.out.println(userQuery.getRealName()+"name");
			jpql.append(" o.realName like ?");
			params.add("%"+ userQuery.getRealName()+ "%");
		}
		if(userQuery.getUserType()!=null && !"".equals(userQuery.getUserType())){
			if(params.size()>0) jpql.append(" and ");
			jpql.append(" o.userType = ?");
			params.add(userQuery.getUserType());
		}
		PageView<User> pageView=new PageView<User>(7,page);
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("id", "desc");
		pageView.setQueryResult(userService.getScrollData(User.class,pageView.getFirstResult(), 
				pageView.getMaxresult(), jpql.toString(), params.toArray(), orderby));
		ActionContext.getContext().put("pageView", pageView);
		return "list" ;
		}
	
	public IUserService getUserService() {
		return userService;
	}
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public UserQuery getUserQuery() {
		return userQuery;
	}
	public void setUserQuery(UserQuery userQuery) {
		this.userQuery = userQuery;
	}
}
